interface ListFormat
{
    [key : string] : any;
}

export class ResManager
{
    private static instance = null;
    public static getInstance() : ResManager
    {
        if(null == this.instance){
            this.instance = new ResManager();
        }
        return this.instance;
    }

    private spriteFrameList : ListFormat = {};

    private getPathAndSuf (path : string){
        var index = path.indexOf(".");
        var plistPath = path;
        var suffex = "";
        if( index != -1 ){
            plistPath = path.substring( 0, index );
            suffex = path.substring(index+1, path.length - index );
        }
        return {"path":plistPath, "suffex":suffex};
    };
    //加载SpriteFrame
    public getSpriteFrameByPath (_spath : string, fn : Function | null) {
        let retps = this.getPathAndSuf(_spath);
        let plistPath = retps.path;
        if(this.spriteFrameList[plistPath] != null){
            if(null != fn){
                fn(this.spriteFrameList[plistPath]);
            }
        }
        cc.log("ResManager getSpriteFrameByPath path: " + plistPath );
        let self = this;
        cc.resources.load(plistPath, cc.SpriteFrame, (err, spriteFrame)=>{
            if(err == null){
                self.spriteFrameList[plistPath] = spriteFrame;
                if(fn){ fn(spriteFrame); }
            }else{
                cc.error(err, spriteFrame);
            }
        });
    }
    //释放SpriteFrame
    public releaseSpriteFrameByPath ( _spath: string){
        var index = _spath.indexOf(".");
        var plistPath = _spath;
        if( index != -1 ){
            plistPath = _spath.substring( 0, index );
        }

        var asset = this.spriteFrameList[plistPath];
        if( asset != null ){
            cc.assetManager.releaseAsset(asset);
            delete this.spriteFrameList[plistPath];
        }
    }
}
